<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/7
 * Time: 17:56
 */

namespace app\api\model;


use app\admin\model\BydAddress;
use app\admin\model\BydDesfollow;
use app\admin\model\BydDesigner;
use app\admin\model\BydDistri;
use think\Model;
use think\Cache;
class BydUser extends Model
{
// 表名
    protected $name = 'byd_user';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    /**
     * 编辑资料
     */
    public function editmine($data)
    {
        // $data = $this->request->request('');
        if (!empty($data['mobile'])) {
            $qq = preg_match_all("/^1[34578]\d{9}$/", $data['mobile'], $mobiles);
            if ($qq == 0) {
                $this->error(__('请输入正确的电话号码'));
            }
        }
        if ($data['email']) {
            $result = $this->validate(
                [
                    'email' => $data['email'],
                ],
                [
                    'email' => 'email',
                ]);
            if (true !== $result) {
                // 验证失败 输出错误信息
                $this->error(__('请输入正确的邮箱'));
            }
        }
        $user = new HouseUser;
// save方法第二个参数为更新条件
        $user->save([
            'mobile' => $data['mobile'],
            'username' => $data['nickname'],
            'birthday' => $data['birthday'],
            'gender' => $data['gender'],
            'email' => $data['email'],
            'qian' => $data['qian'],
        ], ['id' => $data['id']]);
        if ($user) {
            return $user;
        } else {
            return null;
        }
    }
    /**
     * 添加/修改地址
     *   user_id  用户id
     *    username姓名
     *    phone电话
     *   title地址
     *   auto 0为不默认1为默认
     *   type0为修改1为添加
     */
    public  function address($data){
        $type=$data['type'];
        $addre=new BydAddress();
        if($type==1){
            if($data['auto']==0){
                if($data['phone']){
                    $qq=preg_match_all("/^1[34578]\d{9}$/", $data['phone'], $mobiles);
                    if($qq==0){
                        $this->error(__('请输入正确的电话号码'));
                    }
                }
                $cre=BydAddress::create($data);//不默认地址直接添加
            }else{
                $where['userid']=$data['userid'];
                $where['auto']=1;
                $find=BydAddress::where($where)->find();
                if($find){
                    $save=$addre->isUpdate(true,$where)->save(['auto'=>0]);//修改之前的默认地址为不默认
                    $cre=BydAddress::create($data);//添加默认地址
                }else{
                    $cre=BydAddress::create($data);//直接添加默认地址
                }
            }
        }else{
            $wh['id']=$data['id'];
            $save['phone']=$data['phone'];
            $save['username']=$data['username'];
            $save['title']=$data['title'];
            $save['address']=$data['address'];
            $save['auto']=$data['auto'];
            if($data['phone']){
                $qq=preg_match_all("/^1[34578]\d{9}$/", $data['phone'], $mobiles);
                if($qq==0){
                    $this->error(__('请输入正确的电话号码'));
                }
            }
            if($data['auto']==1){
                $ww['auto']=1;
                $ww['userid']=$data['userid'];
                $au=$addre->where($ww)->find();
                if($au){
                    $cres=$addre->isUpdate(true,$ww)->save(['auto'=>0]);//修改地址信息
                }
            }
            $cre=$addre->isUpdate(true,$wh)->save($save);//修改地址信息
        }
        if($cre){
            return $cre;
        } else {
           return null;
        }
    }
    /**
     * 地址列表
     */
    public static function addressshow($data){
        $arr=BydAddress::where('userid','=',$data['id'])->select();
        if($arr){
            return $arr;
        } else {
            return null;
        }
    }
    /**
     * 关注
     * 设计师id
     */
    public static function desfollow($data){
        $add=new BydDesfollow();
        $add->data([
            'user_id'  => $data['user_id'],
            'tes_id' => $data['tes_id'],
        ]);
        $add->save();
        if($add){
            $int=BydDesigner::where($data['text_id'])->setInc('follow',1);//关注人数+1
            return $int;
        }else{
            return  null;
        }
    }

}